package com.sinodata.bsm.cicp.collector.sqlserver;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * <p>Title:SQL Server table </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author majf
 * @version 1.0
 */
public class Table {
    private String name; //unique in database
    private String owner; //owner name
    private String createTime;//create time

    private int rowCount; //total number of records
    private int reservedSize;//total size of reserved space
    private int dataSize;//used space for data
    private int indexSize;//used space for index
    private int unusedSize;//free reserved space

    public Table(String name, String owner, String createTime) {
        this.name = name;
        this.owner = owner;
        this.createTime = createTime;
    }

    public Table(String name, int rowCount, int reservedSize, int dataSize, int indexSize, int unusedSize) {
        this.name = name;
        this.rowCount = rowCount;
        this.reservedSize = reservedSize;
        this.dataSize = dataSize;
        this.indexSize = indexSize;
        this.unusedSize = unusedSize;
    }

    public static List getTablesFromDb(Connection conn, String dbName) throws Throwable {
        if (conn == null)
            return null;
        List tableList = new ArrayList();
        Statement stmt = null;
        ResultSet rst = null;
        String sql = "select o.name, u.name, convert(varchar, o.crdate, 120)" + " from " + dbName + ".dbo.sysobjects o, " + dbName + ".dbo.sysusers u" + " where o.xtype = 'U' and o.uid = u.uid " + " order by o.name";
        try {
            stmt = conn.createStatement();
            rst = stmt.executeQuery(sql);
            while (rst.next()) {
                String tableName = rst.getString(1);
                String ownerName = rst.getString(2);
                String createTime = rst.getString(3);
                tableList.add(new Table(tableName, ownerName, createTime));
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (rst != null) {
                try {
                    rst.close();
                } catch (SQLException ex) {
                    throw ex;
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException ex) {
                    throw ex;
                }
            }
        }
        return tableList;
    }

    public void setMetricValueFromDb(Connection conn) throws Throwable {
        if (conn == null)
            throw new IllegalArgumentException("Connection is null.");

        CallableStatement cstmt = null;
        ResultSet rst = null;
        String sql = "sp_spaceused " + this.name + "";
        try {
            cstmt = conn.prepareCall(sql);
            cstmt.setQueryTimeout(10);
            rst = cstmt.executeQuery(sql);
            while (rst.next()) {
                this.rowCount = Integer.parseInt(cutUnit(rst.getString(2)));
                this.reservedSize = Integer.parseInt(cutUnit(rst.getString(2)));
                this.dataSize = Integer.parseInt(cutUnit(rst.getString(2)));
                this.indexSize = Integer.parseInt(cutUnit(rst.getString(2)));
                this.unusedSize = Integer.parseInt(cutUnit(rst.getString(2)));
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (rst != null) {
                try {
                    rst.close();
                } catch (SQLException ex) {
                    throw ex;
                }
            }
            if (cstmt != null) {
                try {
                    cstmt.close();
                } catch (SQLException ex) {
                    throw ex;
                }
            }
        }

    }

    private String cutUnit(String value) {
        return value.substring(0, value.length() - 3);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public int getRowCount() {
        return rowCount;
    }

    public void setRowCount(int rowCount) {
        this.rowCount = rowCount;
    }

    public int getReservedSize() {
        return reservedSize;
    }

    public void setReservedSize(int reservedSize) {
        this.reservedSize = reservedSize;
    }

    public int getDataSize() {
        return dataSize;
    }

    public void setDataSize(int dataSize) {
        this.dataSize = dataSize;
    }

    public int getIndexSize() {
        return indexSize;
    }

    public void setIndexSize(int indexSize) {
        this.indexSize = indexSize;
    }

    public int getUnusedSize() {
        return unusedSize;
    }

    public void setUnusedSize(int unusedSize) {
        this.unusedSize = unusedSize;
    }
}
